package com.eibus.applicationconnector.dotnet;

import junit.framework.TestCase;
import com.eibus.connector.nom.Connector;
import com.eibus.exception.TimeoutException;
import com.eibus.util.system.EIBProperties;
import com.eibus.xml.nom.Find;
import com.eibus.xml.nom.Node;
import com.eibus.xml.xpath.XPath;

//@Author	: Lejil
//@Class	: TigerRuntimeTest

/**
 *	This class Invokes the methods
 *		TigerRuntime Methods through .NETCall Connector
 *				For all the dataTypes
 */


 /*
 *
 * Java Classes are Generated for Northwind  MethodSets
 *	  * Employees.java
 *	  * Manager.java
 *
 *	Compile the genrated classes an put this in ClASSPATH
 *
 *
 *
 * TIGER - c# wrapper classes are generated and build to TigerRuntime.dll
 *
 *       * Registered the Assembly in Gac // gacutil -i TigerRuntime.dll

 *		 * Exposed methods grouping for differnet catogories
 *
 *
 * .NET Connector -
 *
 *		  Using the metaData Explorer of the .NET Call Connector, web methods
 *			are generated.
 *		  Invoke this methods through .NET Call Connecotr.
 *
 *
 * TIGER COM
 *		.NET Callable wrappers are generated for TIGERCOM, with strong naming
 *
 *		 tlbimp /out:COMJAVALib.dll /keyfile:tiger.snk  TigerCom.dll
 *
 *		Register the Assembly in GAC
 *
 *		gacutil -i COMJAVALib.dll
 *
 *
 */

 public class TigerRuntimeForNorthwindMethodsTestCase extends TestCase
 {

	Connector  connector;


	protected void setUp()
	{
		try
		{
			connector = Connector.getInstance("DotNET Client");
			if (!connector.isOpen())
				connector.open();

		}
		catch(Exception exception)
		{
			fail("Could not get instance of Connector. "+exception.getMessage());
		}
	}

	protected void tearDown()
	{

	}


	public TigerRuntimeForNorthwindMethodsTestCase(String name)
	{
		super(name);
	}

	public static void main(String[] args)
	{
		junit.textui.TestRunner.run(TigerRuntimeForNorthwindMethodsTestCase.class);
	}



	public void  sendRequest(String methodName)
	{
		try
		{
                String ldapSuffix = EIBProperties.getProperty(EIBProperties.LDAP_ROOT);
                
				int method = connector.createSOAPMethod("http://schemas.cordys.com/1.0/dotnet/TigerRuntime",methodName);                
				int response = connector.sendAndWait(Node.getRoot(method));
				checkforSoapFault(response);
				compareResult(response,methodName);
				Util.delete(Node.getRoot(response));
				Util.delete(Node.getRoot(method));
	
		}
		catch(TimeoutException tex)
		{
			fail("Tiger Runtime Methods failed for the Method  :- " + methodName +"  Exception  :-" +tex.toString());
		}
		catch(Exception ex)
		{
			fail("Tiger Runtime Methods failed for the Method  :- " + methodName +"  Exception  :-" +ex.toString());
		}
		

	}

	public  boolean checkforSoapFault(int response) throws Exception
	{
		try
		{
			int  faultNode = XPathUtil.getSOAPFault(response);

			if ( faultNode != 0)
			{  
				fail("Tiger Runtime Methods failed with SOAP Fault :-"+Node.writeToString(faultNode,true));
			}
		}
		catch(Exception ex)
		{
			throw ex;
		}
		return true;
	}



	public void compareResult(int response,String methodName) throws Exception
	{

		String  searchCriteria = "?<"+methodName+"><"+methodName+">";
        //String  searchCriteria = ".//"+methodName+"/"+methodName;
		try
		{
			int resultNode = Find.firstMatch(response,searchCriteria);
            //int resultNode = new XPath(searchCriteria).firstMatch(response, null);
			String result  = Node.getDataWithDefault(resultNode,"false").toLowerCase();

			if (! result.equals("true"))
			{
				fail("Tiger Runtime failed for the  following method :-"+ methodName);
			}
		}
		catch(Exception ex)
		{
			throw ex;
		}
	}


	public void testGetEmployee()
	{
		try
		{
			sendRequest("getEmployee");
		}
		catch(Exception ex)
		{
			fail("Tiger Runtime failed for the  following method :- getEmployee");
		}

	 }

	public void testGetEmployees()
	{
		try
		{
			sendRequest("getEmployees");
		}
		catch(Exception ex)
		{
			fail("Tiger Runtime failed for the  following method :-getEmployees");
		}

	}

 }
